// pages/card/index.js
import {navTo,navToTab,onInput,onClick,showText,total} from '../../utils/reuse'
import {getStore,statusShop} from '../../api/index'
import {baseURL} from '../../utils/request'
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    checked:false,
    list:[],
    Allcheck:false,
    countMax:0,
    priceMax:0
  },
  // 全选
  checkedAll(event) {
    var arr = this.data.list
    arr.forEach(item=>{
      item.goods_state = event.detail
     })
    wx.setStorageSync('cart', arr)
    this.setData({
      Allcheck: event.detail,
      list: arr
    });
    this.MaxpriceFn()
    this.MaxCpuntFn()
  },
  // 全选渲染
  checkChange(){
    var arr = this.data.list
    var allShow = arr.find(item=>item.goods_state == false)
    if(!allShow){
      this.setData({
        Allcheck:true
      })
    }else{
      this.setData({
        Allcheck:false
      })
    }
  },
  // 单选
  checkboxFn(event){
    var id = event.currentTarget.dataset.id
    var arr = this.data.list
    var arrIndex = arr.findIndex(item=>item.goods_id == id)
    arr[arrIndex].goods_state = event.detail
    this.setData({
      list:arr
    })
    this.MaxpriceFn()
    this.MaxCpuntFn()
    this.checkChange()
    wx.setStorageSync('cart', arr)

  },
  // 数量
  countFn(event){
    var arr = this.data.list
    var id = event.currentTarget.dataset.id
    var countIndex = arr.findIndex(item=>item.goods_id == id)
    arr[countIndex].goods_count = event.detail
    this.MaxpriceFn()
    this.MaxCpuntFn()
    this.setData({
      list:arr
    })
    wx.setStorageSync('cart', arr)

  },
  // 总数量
  MaxCpuntFn(){
    var arr = this.data.list
    var num = 0
    arr.forEach(item=>{
      if(item.goods_state){
        num += item.goods_count
      }
    })
    this.setData({
      countMax:num
    })
  },
  // 价格
  MaxpriceFn(){
    var arr = this.data.list
    var num = 0
    arr.forEach(item=>{
      if(item.goods_state){
        num += parseFloat(item.goods_price) * parseFloat(item.goods_count)
      }
    })
    this.setData({
      priceMax:num.toFixed(2)
    })
  },
  // 更新数量
  pageNumFn(){
    var carttotal = wx.getStorageSync('cart').length || 0
    wx.setTabBarBadge({
      index:2,
      text: String(carttotal)
    })
  },
  //删除
  removeFn(){

    var arr = this.data.list
    var removeNum = 0
    arr.forEach(item=>{
      if(item.goods_state){
        removeNum += 1
      }
    })
    if(removeNum<1){
      showText('选择要删除的商品！',2)
    }else{
      Dialog.confirm({
        title: '确定要删除这' + removeNum + '项吗？'
      })
      .then((res) => {
          var newArr = arr.filter(item=>!item.goods_state)
          this.setData({
            list:newArr
          })
          this.MaxpriceFn()
          this.MaxCpuntFn()
          wx.setStorageSync('cart', newArr)
          this.checkChange()
          this.pageNumFn()
      })
      .catch((res) => {
        
      });
    }
  },
  submitFn(){
    if(this.data.countMax>0){
      var arr = this.data.list
      wx.navigateTo({
        url: '/pages/tickets_order/index?id=' + 1,
      })
      var res = []
      arr.forEach(item=>{
        if(item.goods_state){
          res.push(item)
        }
      })
      wx.setStorageSync('goods_order', res)
      wx.setStorageSync('priceMax', this.data.priceMax)
    }else{
      showText('请选中商品！',2)
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },
  click(key){
    onClick(key,this)
  },
  input(key){
    onInput(key,this)
  },
  nav(key){
    navTo(key)
  },
  navtab(e){
    navToTab(e)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var list = wx.getStorageSync('cart')
    if(!list.length <= 0)
    this.setData({
      list:list
    })
    this.MaxpriceFn()
    this.MaxCpuntFn()
    this.pageNumFn()
    this.checkChange()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})